<?php

namespace App\Http\Controllers\Admin;

use Validator;
use App\Category;
use App\ShowPosition;
use Illuminate\Http\Request;
use App\MyTools\ThumbnailImage;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    public function index(Request $request)
    {
        return view('admin.article.index', [
            'request' => $request
        ]);
    }

    public function add(Request $request, ThumbnailImage $img)
    {
        if ($request->isMethod('post')) {
            $data = $request->except(['_token']);
            $rules = [
                'name' => 'required',
                'type' => 'in:0,1',
                'is_nav' => 'in:0,1'
            ];
            $messages = [
                'name.required' => '请输入栏目名！',
                'type.in' => '请选择栏目类型！',
                'is_nav.in' =>'请选择是否显示！'
            ];
            if ($data['type'] == '1') {
                $rules['content'] = 'required';
                $messages['content.required'] = '请输入栏目内容！';
            }
            $validator = Validator::make($data, $rules, $messages);
            if ($validator->fails()) {
                $return = [
                    'status' => 'n',
                    'info' => $validator->errors()->first()
                ];
                return response()->json($return);
            }


        }
        $categories = Category::orderBy('fullpath', 'asc')
            ->where('type', '0')->get();
        $showPositions = ShowPosition::get();
        return view('admin.article.add', [
            'request' => $request,
            'categories' => $categories,
            'showPositions' => $showPositions
        ]);
    }
}